/**
 * 商品规格SKU归并算法
 */
class SkuMergeAlgo {
    constructor(option={}) {
        //规格视图
        this.specVo = []
        //SKU集合对象
        this.skuSetDto = []
        //构造表头
        this.tableColumns = this.tableColumns?this.tableColumns:[]
        //商品编号
        this.skuNo = option.skuNo?option.skuNo:null
        //商品型号
        this.skuModel = option.skuModel
        //成本价
        this.costPrice = option.costPrice?option.costPrice:0
        //出售价
        this.sellingPrice = option.sellingPrice?option.sellingPrice:0
        //简称
        this.shortName = option.shortName?option.shortName:0
          //划线价
          this.crossedPrice = option.crossedPrice?option.crossedPrice:0
        //速记码PLU
        this.pluCode = option.pluCode?option.pluCode:0
        //产地
        this.productionPlace = option.productionPlace?option.productionPlace:null
        //长(米)
        this.vLenght = option.vLenght?option.vLenght:0
        //宽(米)
        this.vWide = option.vWide?option.vWide:0
        //高(米)
        this.vHight = option.vWide?option.vHight:0
        //销售价
        this.salesPrice = option.salesPrice?option.salesPrice:0
         //货架号
         this.shelfNumber = option.shelfNumber?option.shelfNumber:0
        //批发价
        this.wholesalePrice=option.wholesalePrice?option.wholesalePrice:0,
        //发布状态
        this.skuStatus = option.skuStatus?option.skuStatus:1
        //赠送积分
        this.giveScore = option.giveScore?option.giveScore:0
        //商品编号69码
        this.eanCode = option.eanCode?option.eanCode:null
        //商品编号69码
        this.skuWeight = option.skuWeight?option.eanCode:null

        this.inventory = option.inventory


        // this.eanCode = option.eanCode

        // this.skuName = option.skuName

        // this.eanCode = option.eanCode

       
    }

    //规格参数转二维数组
    convert() {
        const towDimArray = []
        this.specVo.forEach((element,id) => { //规格项
            const specArray = []
            element.values.forEach((val,n) => { //规格项明细
                //生成规格明细ID
                // let specId = Number([new Date().getTime(),id,n].join(''))
                const spec = {
                    name: element.title,
                    value: val.specValue,
                    specLevel: id+1
                }
                if(val.specId){
                    spec.specId = val.specId
                }
                if(val.skuId){
                    spec.skuId = val.skuId
                }
                if(id === 0 && val.ivu && val.ivu.length>0){
                    spec.album = val.ivu[0].src
                }
                specArray.push(spec)
            })
            towDimArray.push(specArray)
        })
        return towDimArray
    }

    //笛卡尔积计算
    descartes(args) {
        return args.reduce(
            (total, current) => {
                let ret = [];
                total.forEach(a => {
                    current.forEach(b => {
                        ret.push(a.concat([b]));
                    });
                });
                return ret;
            },
            [[]]
        )
    }

    //重构表头
    columns(args) {
        const columnTop = []
        //console.log('重构表头 ->',this.specVo)
        this.specVo.forEach((element,id) => {
            const column = {
                title: element.title,
                key: "specValue",
                width: 120,
                render: (h,params) => {
                    return h('span', params.row.specEntityList[id] ? params.row.specEntityList[id].specValue : '')
                }
            }
            columnTop.push(column)
        })
        //合并集合
        // columnTop.push.apply(columnTop,this.tableColumns)
        return columnTop
    }

    //转换SKU集合
    build(spuName,specVo) {
        console.log('Algorithm.specVo -> ',specVo)
        const result = []
        if(specVo instanceof Array && specVo.length > 0) {
            this.specVo = specVo
            const one = this.descartes(this.convert())
            one.forEach(tow => {
                let skuName = spuName
                let skuModel=''
                let thumbnailUrl = null
                let specList = []
                let isSkuId = true
                tow.forEach(element => {
                    let specObj = {}
                    skuName += '/'+element.value
                    skuModel +=element.value+','
                    specObj.specName = element.name
                    specObj.specValue = element.value
                    specObj.specLevel = element.specLevel
                    specObj.specAlbum = specVo[0].specAlbum

                    if(element.specId){
                        specObj.specId = element.specId
                    }
                    if(element.album){
                        specObj.specAlbum = element.album
                        thumbnailUrl = element.album
                    }
                    if(!element.skuId){
                        isSkuId = false
                    }
                    specList.push(specObj)
                })


                //记录skuModle

                //初始化一个SKU记录
                const sku = {
                    specEntityList: specList,
                    taocanDay: 1
                }
                //SKU主键
                if(isSkuId){
                    sku.skuEntity.skuId = tow[0].skuId
                }
                //SKU主图地址
                if(thumbnailUrl){
                    sku.skuEntity.thumbnailUrl = thumbnailUrl
                }
                result.push(sku)
            })
        }
        //计算SKU集合对象并返回
        this.skuSetDto = result
        //计算SKU表头并返回
        this.tableColumns = this.columns(result)
        return this
    }

    //转换SKU集合
    rebuild(spuName,specVo) {
        const result = []
        if(specVo instanceof Array && specVo.length > 0) {
            this.specVo = specVo
            const one = this.descartes(this.convert())
            one.forEach(tow => {
                let skuName = spuName
                let thumbnailUrl = null
                let specList = []
                let isSkuId = true
                tow.forEach(element => {
                    let specObj = {}
                    skuName += '/'+element.value
                    specObj.specName = element.name
                    specObj.specValue = element.value
                    specObj.specLevel = element.specLevel
                    if(element.specId){
                        specObj.specId = element.specId
                    }
                    if(element.album){
                        specObj.specAlbum = element.album
                        thumbnailUrl = element.album
                    }
                    if(!element.skuId){
                        isSkuId = false
                    }
                    specList.push(specObj)
                })
                //初始化一个SKU记录
                const sku = {
                    specEntityList: specList,
                    taocanDay: 1
                }
                //SKU主键
                if(isSkuId){
                    sku.skuEntity.skuId = tow[0].skuId
                }
                //SKU主图地址
                if(thumbnailUrl){
                    sku.skuEntity.thumbnailUrl = thumbnailUrl
                }
                result.push(sku)
            })
        }
        //计算SKU集合对象并返回
        this.skuSetDto = result
        //计算SKU表头并返回
        this.tableColumns = this.columns(result)
        return this
    }

    getSkuList() {
        return this.skuSetDto
    }
    
    getColumns() {
        return this.tableColumns
    }
}

export const SkuAlgorithm = SkuMergeAlgo